<template>
    <div>
        <el-tag class="headerSize">用户头像</el-tag>
        <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <el-avatar v-if="imageUrl" :src="imageUrl" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
    </div>
</template>

<script>
    export default {
        name: "HeadPhotoUpload",
        props:{
            path:String
        },
        data() {
            return {
                urlPath:this.$store.state.beginUrl+this.path,
                imageUrl: '',
                dialogVisible: false,
            };
        },
        methods: {
            handleAvatarSuccess(res, file) {
                console.log(file);
                this.imageUrl = URL.createObjectURL(file.raw);
            },



            beforeAvatarUpload(file) {
                console.log(555)
                const isJPG = /^image\/(jpeg|png|jqp|gif)$/.test(file.type)
                const isLt2M = file.size / 1024 / 1024 < 5;
                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 image/jpeg/bmp/jpg/png/gif 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        /*overflow: hidden;*/
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #E6A23C;
        width: 130px;
        height: 130px;
        line-height: 130px;
        font-size: 40px;
    }
    .headerSize{
        font-size: 16px;
        width: 130px;
    }
    .avatar {
        width: 130px;
        height: 130px;
    }
</style>